package com.aptemo.webetek.server.handlers;

import com.aptemo.webetek.client.commands.Action;
import com.aptemo.webetek.client.commands.impl.PaymentAction;
import com.aptemo.webetek.client.exceptions.BackendException;
import com.aptemo.webetek.client.exceptions.WebetekLoginException;
import com.aptemo.webetek.client.exceptions.BackendException.EErrorCodes;
import com.aptemo.webetek.server.datastore.DataStoreAPI;
import com.aptemo.webetek.shared.PaymentBean;

public class CreatePaymentHandler
		extends ActionHandler<PaymentAction, PaymentBean>
{

	public CreatePaymentHandler(DataStoreAPI service)
	{
		super(service);
	}

	public Class<? extends Action<?>> getActionType()
	{
		return PaymentAction.class;
	}

	@Override
	public PaymentBean invoke(PaymentAction action) throws BackendException, WebetekLoginException
	{
		utils.checkLogin();

		// TODO add checks....
		//if (!utils.isTeamOwner(action.getTeamId()))
		if (!utils.isTeamEditor(action.getTeamId()))
		{
			throw new BackendException(EErrorCodes.NOT_TEAM_OWNER);
		}


		PaymentBean user = dataService.createPayment(action.getUserId(), action.getTeamId(), action.getAmount(), action.getType());
		return user;
	}

}
